package com.itheima.web.controller.ums;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.ums.AdminRoleDto;
import com.itheima.service.ums.AdminRoleService;
import com.itheima.utils.JwtUtils;
import com.itheima.vo.ums.AdminRole;
import com.itheima.utils.PageResult;
import com.itheima.utils.ResultData;
import com.itheima.vo.ums.UmsAdmin;
import com.itheima.service.ums.AdminService;
import com.itheima.vo.ums.UmsRole;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {

    @DubboReference
    private AdminService adminService;

    @DubboReference
    private AdminRoleService adminRoleService;

    /**
     * 登录以后返回token
     * @param umsAdmin
     * @return
     */
    @PostMapping("login")
    public ResultData login(@RequestBody UmsAdmin umsAdmin){

        String token = adminService.login(umsAdmin);
        if(token == null){
            return ResultData.error("用户名或密码错误");
        }
        if("0".equals(token)){
            return ResultData.error("该用户未启用");
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("token",token);
        map.put("tokenHead","Bearer ");
        return ResultData.ok("操作成功",map);
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @GetMapping("info")
    public ResultData info(){

        HashMap<String ,Object> map = adminService.info();
        return ResultData.ok("操作成功",map);
    }

    /**
     * 登出功能
     * @return
     */
    @PostMapping("logout")
    public ResultData logout(){
        return ResultData.ok("退出成功",null);
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("list")
    public ResultData list(Integer pageNum,Integer pageSize,String keyword){

        Page<UmsAdmin> umsAdminPage = adminService.getPage(pageNum,pageSize,keyword);
        PageResult page = PageResult.getPage(umsAdminPage);

        return ResultData.ok("操作成功",page);
    }

    /**
     * 修改帐号状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("updateStatus/{id}")
    public ResultData updateStatus(@PathVariable Integer id, Integer status){

        adminService.updateStatus(id,status);
        return ResultData.ok("操作成功",null);
    }

    /**
     * 修改指定用户信息
     * @param id
     * @param umsAdmin
     * @return
     */
    @PostMapping("update/{id}")
    public ResultData update(@PathVariable Integer id,@RequestBody UmsAdmin umsAdmin){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(umsAdmin.getPassword());
        umsAdmin.setPassword(encode);
        adminService.updateById(umsAdmin);
        return ResultData.ok("操作成功",1);
    }

    @PostMapping("delete/{id}")
    public ResultData delete(@PathVariable Integer id){
        adminService.removeById(id);
        return ResultData.ok("操作成功",1);
    }

    /**
     * 用户注册
     * @param umsAdmin
     * @return
     */
    @PostMapping("register")
    public ResultData add(@RequestBody UmsAdmin umsAdmin){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(umsAdmin.getPassword());
        umsAdmin.setPassword(encode);
        adminService.save(umsAdmin);
        return ResultData.ok("操作成功",umsAdmin);
    }

    /**
     * 给用户分配角色
     * @return
     */
    @PostMapping("role/update")
    public ResultData update(Integer adminId,Integer[] roleIds){
        ArrayList<AdminRole> adminRoles = new ArrayList<>();

        adminRoleService.removeByAdminId(adminId);

        for (Integer roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRoles.add(adminRole);
        }

        adminRoleService.saveBatch(adminRoles);
        return ResultData.ok("操作成功",roleIds.length);
    }

    /**
     * 获取指定用户的角色
     */
    @GetMapping("role/{adminId}")
    public ResultData role(@PathVariable Integer adminId){
        List<UmsRole> roles = adminRoleService.getRole(adminId);
        return ResultData.ok("操作成功",roles);
    };
}
